public class leetcode_35搜索插入位置 {
    public static void main(String[] args) {
        leetcode_35搜索插入位置 l = new leetcode_35搜索插入位置();
        int[] nums = {1,3,5,6};
        System.out.println(l.searchInsert(nums, 7));
    }

    public int searchInsert(int[] nums, int target) {
        if (target > nums[nums.length - 1]) {
            return nums.length;
        }
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            int n = nums[mid];
            if (n == target) {
                return mid;
            }
            if (target > n) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }
}
